home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.cs.arizona.edu
/
ftp.cs.arizona.edu.tar
/
ftp.cs.arizona.edu
/
icon
/
newsgrp
/
group98c.txt
/
000143_icon-group-sender _Thu Dec 17 16:31:54 1998.msg
< prev
next >
Wrap
Internet Message Format
|
2000-09-20
|
2KB
Return-Path: <icon-group-sender>
Received: (from root@localhost)
by baskerville.CS.Arizona.EDU (8.9.1a/8.9.1) id QAA26266
for icon-group-addresses; Thu, 17 Dec 1998 16:31:48 -0700 (MST)
Message-Id: <199812172331.QAA26266@baskerville.CS.Arizona.EDU>
Date: Thu, 17 Dec 1998 14:21:23 -0700
From: Steve Wampler <swampler@gemini.edu>
X-Accept-Language: en
To: icon-group@optima.CS.Arizona.EDU
Subject: Re: Small Icon programming problem
Errors-To: icon-group-errors@optima.CS.Arizona.EDU
Status: RO
Ralph Griswold wrote:
>
> Here's a small Icon programming problem for you to tackle:
>
> Write a procedure digsort(i) that returns the integer that
> results from sorting the digits of i, preserving sign. For
> example, digsort(201) should return 12 and digsort(-1042)
> should return -124. You may assume i is an integer.
>
> You could aim for brevity, speed, and/or clarity.
>
> Send solutions and comments to icon-group. The most interesting
> solutions will appear in a future issue of the Icon Analyst and
> sent to icon-group as a package.
What a fun thing to play with! Here's two more solutions. For
my test cases these are the fastest I've been able to come up with.
(I suspect there is a much faster approach involving map(), but
this may just be a pipedream.)
The 2nd is slightly faster, but relies on the trick of returning
a string that looks right (so it's hard to tell that it isn't
returning an integer.)
Performance is affected by the choice of input.
procedure digsort(i)
local s, c
s := ""
i := string(i) # convert to string
every c := !cset(i) do { # get correct order
every s ||:= (find(c,i) & c) # get correct count
}
return integer(s) # convert back to int
end
The next version is barely faster, due to a 'trick' and has an obscure bit
of code in cset(integer(cset(i))):
procedure digsort(i)
local s, c, j
s := ""
i := string(i) # convert to string
every c := !cset(integer(cset(i))) do { # get correct order & toss 0
every s ||:= (find(c,i) & c) # get correct count
}
return s
end
--
Steve Wampler (swampler@gemini.edu)